"""
Case Type   : dolphin
Case Name   : 验证mysql兼容字符函数，部分测试点合理报错
Create At   : 2022.04.06
Owner       : z@wan005
Description :
    1、创建b库
    2、b库下查询dolphin插件
    3、locate(substr,str)函数验证
    4、LOCATE(substr,str,pos)函数验证
    5、清理环境
Expect      :
    1、成功
    2、自动加载成功,无需手动创建
    3、成功
    4、成功，当第3个参数为空字符，打印warning并返回结果
    5、清理环境完成
History     :modified by z@wan005,2022.08.29，om数据库目前实现自动加载dolphin插件，
             因此删除用例中拷贝插件依赖文件步骤
             modified by z@wan005，2022.12.14，修改步骤4断言
             modified by z@wan005,2023.09.22，步骤4中，当第3个参数为空字符，
             打印warning并返回结果
             modified by zhanghuan96,2024.7.3，报错提示变更
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class PluginTest015(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_plugin0015'
        self.db_name_01 = 'db_plugin0015_01'

    def test_dolphin(self):
        text = '----step1:创建b库;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} DBCOMPATIBILITY ='b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '---step2:b库下查询dolphin插件;expect:自动加载成功,无需手动创建---'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual('dolphin', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '-----step3:locate(substr,str)函数验证;expect:成功---'
        self.log.info(text)
        sql_cmd = f'''
            select locate('go','testgood');
            select locate('测试啊','啊');'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        msg = sql_res.splitlines()
        self.log.info(msg)
        self.assertEqual('5', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('0', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '-----step4:LOCATE(substr,str,pos)函数验证;' \
               'expect:成功，当第3个参数为空字符，打印warning并返回结果---'
        self.log.info(text)
        sql_cmd = '''select locate('yy','11testgood',5);'''
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual('0', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        sql_cmd = '''select locate('test','11testgood','');'''
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('WARNING:  invalid input syntax for type integer',
                      sql_res, '执行失败' + text)
        self.assertEqual('0', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        sql_cmd = '''select locate('test','11testgood',2.5);'''
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual('3', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        sql_cmd = '''select locate('test','11testgood',-3);'''
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual('0', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

    def tearDown(self):
        text = '--step5:清理环境;expect:清理环境完成--'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};"
        sql_res = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.log.info('断言teardown成功')
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
